package org.usmile.algorithms.huawei.acm;

import java.util.HashMap;
import java.util.Scanner;

// https://fcqian.blog.csdn.net/article/details/128249555
public class _挑选字符串 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        System.out.println(getResult(a, b));
    }

    public static int getResult(String a, String b) {
        // indexes对象记录字符串b中每个字符的索引
        HashMap<Character, Integer> indexes = new HashMap<>();
        for (int i = 0; i < b.length(); i++) {
            Character c = b.charAt(i);
            indexes.put(c, i); // B不会存在重复字母
        }

        // count对象用于记录遍历字符串a每个字符串过程中，统计到的符合顺序要求的字符串b中字符出现次数
        int[] count = new int[b.length()];
        for (int i = 0; i < a.length(); i++) {
            Character c = a.charAt(i);
            if (indexes.containsKey(c)) {
                int index = indexes.get(c);
                // 下面判断逻辑请看图解
                if (index == 0 || count[index] < count[index - 1]) {
                    count[index]++;
                }
            }
        }

        return count[count.length - 1];
    }
}
